#ifndef __OS_SYS_H__
#define __OS_SYS_H__
#include <stdint.h>
#include "os_sys.h"
#ifdef _WIN32
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <Windows.h>
#include <stdio.h>
#include <stdbool.h>
#include "lfqueue.h"
#define RQ_MALLOC malloc
#define RQ_FREE  free
#define MUTEX_CREATE(lock) pthread_mutex_init(&lock, NULL)
#define MUTEX_DESTROY(lock) pthread_mutex_destroy(&lock)
#define MUTEX_LOCK(lock) pthread_mutex_lock(&lock)
#define MUTEX_UNLOCK(lock) pthread_mutex_unlock(&lock)
#define SEM_CREATE(sem, value) sem_init(&sem, 0, value)
#define SEM_DESTROY(sem) sem_destroy(&sem)
#define SEM_POST(sem) sem_post(&sem)
#define SEM_PEND_FOREVER(sem) sem_wait(&sem)
typedef pthread_mutex_t rq_lock;
typedef sem_t rq_sem;
#define SLEEP_MS(n) Sleep(n)
typedef int slpManLpState;
typedef FILE *OS_FILE;
#define OS_FOPEN fopen
#define OS_FCLOSE fclose
#define OS_FSEEK fseek
#define OS_FTELL ftell
#define OS_FWRITE fwrite
#define OS_FREAD fread
int32_t OS_FSIZE(OS_FILE fp);
#define OS_FREMOVE remove
#define OS_GETTICKCNT GetTickCount
#define MSG_RET_OK 0
typedef int MSG_RET;
typedef struct MSG_QUEUE {
  void* data;
  lfqueue_t queue;
  uint32_t item_size;
} MSG_QUEUE_t, *OS_MSGQUE;

#else
#include "liot_os_pal.h"
#include "slpman.h"
#include "lfs.h"
#include "liot_fs_pal.h"
#include "liot_os_pal.h"
#include "cmsis_os2.h"
#include "FreeRTOS.h"
#include "queue.h"
#define RQ_MALLOC Liot_Malloc
#define RQ_FREE  Liot_Free
#define MUTEX_CREATE(lock) (lock = Liot_MutexCreate())
#define MUTEX_DESTROY(lock) Liot_MutexDestroy(lock)
#define MUTEX_LOCK(lock) Liot_MutexLock(lock)
#define MUTEX_UNLOCK(lock) Liot_MutexUnlock(lock)
#define SEM_CREATE(sem, value) (sem = Liot_SemaphoreNew(10, value, NULL))
#define SEM_DESTROY(sem) Liot_SemaphoreDelete(sem)
#define SEM_POST(sem) Liot_SemaphoreRelease(sem)
#define SEM_PEND_FOREVER(sem) Liot_SemaphoreAcquire(sem, 0)
#define SLEEP_MS(n) Liot_SleepMs(n)
typedef Liot_MutexId_t rq_lock;
typedef Liot_SemaphoreId_t rq_sem;
typedef LIOTFILE OS_FILE;
#define OS_FOPEN Liot_Fopen
#define OS_FCLOSE Liot_Fclose
#define OS_FSEEK Liot_Fseek
#define OS_FTELL Liot_Ftell
#define OS_FWRITE Liot_Fwrite
#define OS_FREAD Liot_Fread
#define OS_FSIZE Liot_Fsize
#define OS_FREMOVE Liot_Fremove
#define OS_GETTICKCNT osKernelGetTickCount
#define OS_MSGQUE QueueHandle_t
#define MSG_RET_OK true
typedef bool MSG_RET;
#endif
int SEM_PEND(rq_sem, uint32_t time);
OS_MSGQUE MSG_QUEUE_CREATE(uint32_t count, uint32_t item_size);
void MSG_QUEUE_DESTROY(OS_MSGQUE q);
MSG_RET MSG_QUEUE_SEND(OS_MSGQUE q, const void* item, uint32_t wait_ticks);
MSG_RET MSG_QUEUE_RECV(OS_MSGQUE q, void *item, uint32_t wait_ticks);
#define SAFE_DIVIDE(a, b) ((b != 0) ? (a / b) : a)
#endif


